from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.orm.session import Session

from airflow.models.base import Base
from airflow.models.idata_project_user import ProjectUser
from airflow.utils.session import provide_session


class Env(Base):
    """the class to get user info."""

    __tablename__ = "idata_env"
    __table_args__ = {"useexisting": True}

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255))
    type = Column(Integer)
    etl_path = Column(String(255))
    common_jar_path = Column(String(255))
    kafka_jar_path = Column(String(255))
    temporary_file_path = Column(String(255))
    jdbc_jar_path = Column(String(255))
    mongodb_jar_path = Column(String(255))
    checkpoint_path = Column(String(255))
    decoder_path = Column(String(255))
    hive_jar_path = Column(String(255))
    es_jar_path = Column(String(255))
    binary_path = Column(String(255))
    docker_image = Column(String(255))

    @staticmethod
    @provide_session
    def get_env(env_id, session: Session = None):
        return session.query(Env).filter(Env.id == env_id).first()

    @staticmethod
    @provide_session
    def get_envs(env_type, session: Session = None):
        q = session.query(Env)
        if env_type and env_type != '':
            q = q.filter(Env.type == env_type)
        return q.all()

    @staticmethod  # 静态方法
    @provide_session
    def get_envs_page(page, env_type, session: Session = None):
        q = session.query(Env).order_by(Env.id)
        if env_type and env_type != '':
            q = q.filter(Env.type == env_type)
        return q.paginate(page, 10, error_out=False)

    @staticmethod
    @provide_session
    def add_env(env, session: Session = None):
        return session.add(env)

    @staticmethod
    @provide_session
    def delete_env(env_id, session: Session = None):
        session.query(Env).filter(Env.id == env_id).delete()

    @staticmethod
    @provide_session
    def update_env(env, session: Session = None):
        result = session.query(Env).filter(Env.id == env.id).first()
        if result is None:
            return
        else:
            return session.merge(env)

    def to_json(self):
        if self is None:
            return None
        data = {
            'id': self.id,
            'name': self.name,
            'type': self.type,
            'etl_path': self.etl_path,
            'common_jar_path': self.common_jar_path,
            'kafka_jar_path': self.kafka_jar_path,
            'temporary_file_path': self.temporary_file_path,
            'jdbc_jar_path': self.jdbc_jar_path,
            'mongodb_jar_path': self.mongodb_jar_path,
            'checkpoint_path': self.jdbc_jar_path,
            'decoder_path': self.jdbc_jar_path,
            'hive_jar_path': self.jdbc_jar_path,
            'es_jar_path': self.jdbc_jar_path,
            'binary_path': self.jdbc_jar_path,
            'docker_image': self.jdbc_jar_path,
        }
        return data